clear all
set more off, perm
set mem 10000000
set matsize 10000
version 13

*********************************************************
** RDROBUST placebo test for 2011 nighttime brightness **
*********************************************************

** Set file paths
do "$path_code/paths.do"

**************************************************************
**************************************************************

** Estimate 10000 RDs with 150-person bandwidths, with thresholds picked between 150 and 1000
use "$sens/RDROBUST_placebo_test_lights.dta", clear
keep if tot_p < 1500
local yvar = "lights_max2011_hat"
local controls = "lights_max1998 lights_max1999 lights_max2000 lights_max2001 lights_max2002 lights_max2003 lights_max2004 lights_max2005" 
local fe = "STFE*"
local kernel = "tri"
local bwmethod = "mserd"
local vce = ""
local poly = 1

	// Set random seed and ensure sufficient observations
set seed 91784
gen row = _n
if _N<10000 {
	set obs 10000
}

	// Generate placebo cutoffs
gen placebo_low  = round(151+(275-151)*runiform(),1) if row<=10000
gen placebo_high = round(325+(1000-325)*runiform(),1) if row<=10000
gen placebo_hilo = (round(0+(801-0)*runiform(),1)<125) if row<=10000
gen placebo = placebo_hilo*placebo_low + (1-placebo_hilo)*placebo_high if row<=10000
replace placebo = placebo - 0.5
drop placebo_low placebo_high placebo_hilo

	// Create variables to store regression results
gen yvar = ""
gen ifs = ""
gen control = ""
gen fe = ""
gen kernel = ""
gen bwmethod = ""
gen vce = ""
gen polynomial_order = .
gen beta_conv = .
gen beta_robust = .
gen se_conv = .
gen se_robust = .
gen pval_conv = .
gen pval_robust = .
gen lci_conv = .
gen uci_conv = .
gen lci_robust = .
gen uci_robust = .
gen bw_lo = .
gen bw_hi = .
gen nobs_orig = .
gen nobs_left = .
gen nobs_right = .
gen ndist = . 
gen ymean = .


	// Store empty dataset of placebo results, to populate below
sort placebo
save "$sens/RDROBUST_placebo_test_lights_results.dta", replace

	// Run placebo RDROBUSTs
use "$sens/RDROBUST_placebo_test_lights_results.dta", clear	
forvalues i = 1/10000 {

	// Set placebo threshold 
	local PLACEBO = placebo[`i'] 

	// If it's a new placebo threshold, run RDs
	if `PLACEBO'>placebo[`i'-1] | `i'==1 {

		// Clear break flag
		local loop_flag = ""

		// Clear ereturn list
		ereturn clear

		// Run placebo RD
		cap rdrobust `yvar' tot_p, c(`PLACEBO') covs(`controls' `fe') kernel(`kernel') bwselect(`bwmethod') p(`poly') all `vce'
	
		// Flag iterations that break rdrobust
		if e(tau_bc)==. {
			local loop_flag = "  rdrobust broke!"
		}
	
		// Generate in-sample indicator
		cap drop temp_in_reg
		cap gen temp_in_reg = inrange(tot_p,`PLACEBO'-e(h_l),`PLACEBO'+e(h_r))
		cap sum `yvar' if temp_in_reg==1 & tot_p<`PLACEBO'
		local r_mean = r(mean)

		// Store results
		qui replace yvar = "`yvar'" if _n==`i'
		qui replace ifs = "pop_mismatch20==0 & lights_diff<20" if _n==`i'
		qui replace control = "`controls'" if _n==`i'
		qui replace fe = "`fe'" if _n==`i'
		cap replace kernel = e(kernel) if _n==`i'
		cap replace bwmethod = e(bwselect) if _n==`i'
		cap replace vce = "`vce'" if _n==`i'
		cap replace polynomial_order = e(p) if _n==`i'
		cap replace beta_conv = e(tau_cl) if _n==`i'
		cap replace beta_robust = e(tau_bc) if _n==`i'
		cap replace se_conv = e(se_tau_cl) if _n==`i'
		cap replace se_robust = e(se_tau_rb) if _n==`i'
		cap replace pval_conv = e(pv_cl) if _n==`i'
		cap replace pval_robust = e(pv_rb) if _n==`i'
		cap replace lci_conv = e(ci_l_cl) if _n==`i'
		cap replace uci_conv = e(ci_r_cl) if _n==`i'
		cap replace lci_robust = e(ci_l_rb) if _n==`i'
		cap replace uci_robust = e(ci_r_rb) if _n==`i'
		cap replace bw_lo = e(h_l) if _n==`i'
		cap replace bw_hi = e(h_r) if _n==`i'
		cap replace nobs_orig = e(N) if _n==`i'
		cap replace nobs_left = e(N_h_l) if _n==`i'
		cap replace nobs_right = e(N_h_r) if _n==`i'
		cap unique stdt if temp_in_reg==1
		cap replace ndist = r(unique) if _n==`i'
		cap replace ymean = `r_mean' if _n==`i'

	}
	
	// If it's a repeat placebo threshoold, carry forward results 
	else if `PLACEBO'==placebo[`i'-1] {

		// Store results
		foreach v of varlist yvar-ymean {
			cap replace `v' = `v'[_n-1] if _n==`i'
		}
	}
	
	// Save updated dataset
	qui save "$sens/RDROBUST_placebo_test_lights_results.dta", replace

	// Report intermediate output
	di "`i'  `loop_flag'"
}

	// Save results
keep placebo yvar-ymean
keep in 1/10000
compress
save "$sens/RDROBUST_placebo_test_lights_results.dta", replace

**************************************************************
**************************************************************

